﻿
$(function () {
   Show();
});

    //显示质检组
function Show() {
    layui.use(['form', 'table'], function () {
        var table = layui.table;
        var Num = $("#Num").val();
        var ZName = $("#ZName").val();
        console.log(Num)
        // 创建表格实例
        $.ajax({
            url: "/GualityGroup/LedgerInfoShow",
            type: "get",
            data: {
                pageindex: 1,
                pagesize: 100,
                gualityGroupNumbering: Num,
                gualityGroupName: ZName,
                token: localStorage.getItem("token")
            },
            success: function (data) {
                data = JSON.parse(data);
                console.log(data.data.plist);
                table.render({
                    elem: '#carriertable',  // 表格元素的选择器
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    data: data.data.plist,
                    toolbar: '#toolbarDemo',
                    defaultToolbar: ['exports'],
                    cols: [[
                        { type: 'checkbox', fixed: 'left' },
                        { field: 'gualityGroupName', title: '质检组名称' },
                        { field: 'gualityGroupNumbering', title: '质检组编号' },
                        {
                            field: 'addTime', title: '添加时间', templet: function (d) {
                                return formatTime(d.addTime); // 调用自定义的时间格式转换函数
                            }
                        },
                        { field: 'addName', title: '创建人' },
                        { field: 'gualityGroupRemark', title: '备注' },
                        {
                            field: '', title: '操作', templet: function (d) {
                                return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.gualityGroupId + ')"><i class="layui-icon layui-icon-edit"></i>编辑</a> ' +
                                    '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(' + d.gualityGroupId + ')"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                            }

                        }
                    ]],
                });
            },
            error: function () {
                console.log("请求失败");
            }
        });
    });
    }


    //删除质检组
    window.deleteData = function (id) {
        console.log(id);
        layer.confirm('确定要删除该条数据吗？', function (index) {
            $.ajax({
                type: "Post",
                url: "/GualityGroup/GualityGroupDelete",  // 替换为实际的后台删除数据接口地址
                data: { data: JSON.stringify({ gualityGroupId: id }), token: localStorage.getItem("token") },
                success: function (res) {
                    var res = JSON.parse(res)
                    if (res.data >= 1) {
                        layer.alert("删除成功");
                        Show();  // 重新加载数据，刷新表格显示
                    } else {
                        layer.alert("删除失败");

                    }
                },
                error: function () {
                    layer.msg('删除失败，请稍后重试', {
                        time: 2000,
                        icon: 2
                    });
                }
            });
            layer.close(index);
        });
    };

    layui.use('layer', function () {
        var $ = layui.$;
        var layer = layui.layer;
        var util = layui.util;
        var form = layui.form;
        var table = layui.table;

        // 打开添加弹窗的按钮点击事件
        util.on('lay-on', {
            'test-page-custom': function () {
                layer.open({
                    type: 1,
                    title: "新增质检组",
                    content: `
                    <form class="layui-form" style="padding:20px;">
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检组编号</label>
                    <div class="layui-input-inline">
                    <input type="text" name="gualityGroupNumbering" autocomplete="off" id="gualityGroupNumbering" placeholder="请输入质检组编号" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检组名称</label>
                    <div class="layui-input-inline">
                    <input type="text" name="gualityGroupName" id="gualityGroupName" autocomplete="off" placeholder="请输入质检组名称" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-inline">
                    <textarea name="gualityGroupRemark" id="gualityGroupRemark" placeholder="请输入备注" class="layui-textarea"></textarea>
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <div class="layui-input-block">
                    <button class="layui-btn layui-btn-primary" id="cancelBtn">取消</button>
                     <button class="layui-btn" lay-submit lay-filter="demo-login">添加</button>
                    </div>
                    <form>`,
                    success: function () {

                        // 对弹层中的表单进行初始化渲染
                        form.render();
                        // 表单提交事件
                        form.on('submit(demo-login)', function (data) {
                            var field = data.field;
                            console.log(field)
                            $.ajax({
                                url: '/GualityGroup/GualityGroupAdd',
                                type: 'post',
                                data: { data: JSON.stringify(field), token: localStorage.getItem("token") },
                                success: function (res) {
                                    var s = JSON.parse(res)
                                    console.log(res)
                                    if (s.data == 1) {
                                        layer.alert("添加成功");
                                        setTimeout(function () {
                                            location.reload();
                                        }, 2000);
                                    }
                                    else {
                                        layer.alert("添加失败");
                                    }
                                }
                            })
                            return false; // 阻止默认 form 跳转
                        });
                    }
                });

            }
        })
    });
    var obj = {
        gualityGroupId: '',
        gualityGroupName: '',
        gualityGroupNumbering: '',
        gualityGroupRemark: '',
        addName: '',
        addTime: ''
    }
    //反填修改
    window.editData = function (id) {
        // 先根据id获取要编辑的数据，发起Ajax请求
        var form = layui.form;
        $.ajax({
            type: "get",
            url: "/GualityGroup/GualityGroupModel",  // 替换为实际的后台根据id获取数据的接口地址
            data: { gualityGroupId: id, token: localStorage.getItem("token") },
            success: function (res) {
                var res = JSON.parse(res)
                console.log(res)
                $(res.data).each(function () {
                    $('#gualityGroupNumbering').val(this.gualityGroupNumbering)
                    $('#gualityGroupName').val(this.gualityGroupName)
                    $('#gualityGroupRemark').val(this.gualityGroupRemark)
                    $('#addName').val(this.addName)
                    $('#addTime').val(this.addTime)
                    obj.gualityGroupId = this.gualityGroupId
                    obj.gualityGroupNumbering = this.gualityGroupNumbering
                    obj.gualityGroupName = this.gualityGroupName
                    obj.gualityGroupRemark = this.gualityGroupRemark
                    obj.addName = this.addName
                    obj.addTime = this.addTime

                })
            },
        });

        layer.open({
            type: 1,
            title: '编辑质检组',
            content: `<form class="layui-form" style="padding:20px;">
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检组编号</label>
                    <div class="layui-input-inline">
                    <input type="text" name="gualityGroupNumbering" autocomplete="off" id="gualityGroupNumbering" placeholder="请输入质检组编号" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">质检组名称</label>
                    <div class="layui-input-inline">
                    <input type="text" name="gualityGroupName" id="gualityGroupName" autocomplete="off" placeholder="请输入质检组名称" class="layui-input">
                    </div>
                    </div>
                    <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-inline">
                    <textarea name="gualityGroupRemark" id="gualityGroupRemark" placeholder="请输入备注" class="layui-textarea"></textarea>
                    </div>
                    </div>
                    <div class="layui-form-item" style="text-align:center">
                    <button class="layui-btn" lay-submit lay-filter="demo-login">修改</button>
                <button type="reset" class="layui-btn layui-btn-primary">取消</button>
              </div>
                    <form>
                    `,
            area: ['700px', '400px'],
            success: function () {
                var obj1 = {
                    gualityGroupId: '',
                    gualityGroupName: '',
                    gualityGroupNumbering: '',
                    gualityGroupRemark: '',
                    addName: '',
                    addTime: ''
                };
                console.log(obj1)
                // 对弹层中的表单进行初始化渲染
                form.render();
                // 表单提交事件
                form.on('submit(demo-login)', function (data) {
                    var field = data.field;
                    obj1.gualityGroupId = obj.gualityGroupId
                    obj1.gualityGroupName = obj.gualityGroupName
                    obj1.gualityGroupNumbering = field.gualityGroupNumbering
                    obj1.gualityGroupRemark = field.gualityGroupRemark
                    obj1.addName = obj.addName
                    obj1.addTime = obj.addTime
                    $.ajax({
                        url: '/GualityGroup/GualityGroupUpdate',
                        type: 'post',
                        data: { obj1, token: localStorage.getItem("token") },
                        success: function (res) {
                            var res = JSON.parse(res)
                            console.log(field)
                            console.log(res)
                            if (res.data >= 1) {
                                layer.alert("修改成功");
                                setTimeout(function () {
                                    location.reload();
                                }, 2000);
                            }
                            else {
                                layer.alert("修改失败");
                            }
                        }
                    })
                    return false; // 阻止默认 form 跳转
                });
            }
        });
    };


    // 定义时间格式转换函数
    function formatTime(timeStamp) {
        if (timeStamp) {
            var date = new Date(timeStamp);
            var year = date.getFullYear();
            var month = ('0' + (date.getMonth() + 1)).slice(-2);
            var day = ('0' + date.getDate()).slice(-2);
            var hour = ('0' + date.getHours()).slice(-2);
            var minute = ('0' + date.getMinutes()).slice(-2);
            var second = ('0' + date.getSeconds()).slice(-2);
            return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
        }
        return "";
    }

